home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / GIF2RLE.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  9KB  |  277 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to convert GIF file RLE format (utah raster toolkit).             *
  7. * Options:                                     *
  8. * -q : quite printing mode.                             *
  9. * -a : add alpha channel with full coverage.                     *
  10. * -h : on line help.                                 *
  11. ******************************************************************************
  12. * History:                                     *
  13. * 5 Jan 90 - Version 1.0 by Gershon Elber.                     *
  14. *****************************************************************************/
  15.  
  16. #ifdef __MSDOS__
  17. #include <graphics.h>
  18. #include <stdlib.h>
  19. #include <alloc.h>
  20. #include <io.h>
  21. #include <dos.h>
  22. #include <bios.h>
  23. #endif /* __MSDOS__ */
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include "gif_lib.h"
  30. #include "getarg.h"
  31.  
  32. #include "rle.h"               /* The rle tool kit header files. */
  33.  
  34. #define PROGRAM_NAME    "Gif2Rle"
  35.  
  36. #ifdef __MSDOS__
  37. extern unsigned int
  38.     _stklen = 16384;                 /* Increase default stack size. */
  39. #endif /* __MSDOS__ */
  40.  
  41. #ifdef SYSV
  42. static char *VersionStr =
  43.         "Gif library module,\t\tGershon Elber\n\
  44.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  45. static char
  46.     *CtrlStr = "Gif2Rle q%- a%- h%- GifFile!*s";
  47. #else
  48. static char
  49.     *VersionStr =
  50.     PROGRAM_NAME
  51.     GIF_LIB_VERSION
  52.     "    Gershon Elber,    "
  53.     __DATE__ ",   " __TIME__ "\n"
  54.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  55. static char
  56.     *CtrlStr =
  57.     PROGRAM_NAME
  58.     " q%- a%- h%- GifFile!*s";
  59. #endif /* SYSV */
  60.  
  61. /* Make some variables global, so we could access them faster: */
  62. static int
  63.     ImageNum = 0,
  64.     BackGround = 0,
  65.     AlphaFlag = FALSE,
  66.     HelpFlag = FALSE,
  67.     ColorMapSize = 0,
  68.     InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should. */
  69.     InterlacedJumps[] = { 8, 8, 4, 2 };    /* be read - offsets and jumps... */
  70. static GifColorType
  71.     *ColorMap;
  72.  
  73. static void DumpScreen2Rle(GifRowType *ScreenBuffer,
  74.                     int ScreenWidth, int ScreenHeight);
  75.  
  76. /******************************************************************************
  77. * Interpret the command line and scan the given GIF file.              *
  78. ******************************************************************************/
  79. void main(int argc, char **argv)
  80. {
  81.     int    i, j, Error, NumFiles, Size, Row, Col, Width, Height, ExtCode, Count;
  82.     GifRecordType RecordType;
  83.     GifByteType *Extension;
  84.     char **FileName = NULL;
  85.     GifRowType *ScreenBuffer;
  86.     GifFileType *GifFile;
  87.  
  88.     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuitePrint,
  89.         &AlphaFlag, &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  90.         (NumFiles > 1 && !HelpFlag)) {
  91.     if (Error)
  92.         GAPrintErrMsg(Error);
  93.     else if (NumFiles > 1)
  94.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  95.     GAPrintHowTo(CtrlStr);
  96.     exit(1);
  97.     }
  98.  
  99.     if (HelpFlag) {
  100.     fprintf(stderr, VersionStr);
  101.     GAPrintHowTo(CtrlStr);
  102.     exit(0);
  103.     }
  104.     
  105.     if (NumFiles == 1) {
  106.     if ((GifFile = DGifOpenFileName(*FileName)) == NULL) {
  107.         PrintGifError();
  108.         exit(-1);
  109.     }
  110.     }
  111.     else {
  112.     /* Use the stdin instead: */
  113.  
  114. #ifdef __MSDOS__
  115.     setmode(0, O_BINARY);
  116. #endif /* __MSDOS__ */
  117.     if ((GifFile = DGifOpenFileHandle(0)) == NULL) {
  118.         PrintGifError();
  119.         exit(-1);
  120.     }
  121.     }
  122.  
  123.     /* Allocate the screen as vector of column of rows. We cannt allocate    */
  124.     /* the all screen at once, as this broken minded CPU can allocate up to  */
  125.     /* 64k at a time and our image can be bigger than that:             */
  126.     /* Note this screen is device independent - its the screen as defined by */
  127.     /* the GIF file parameters itself.                         */
  128.     if ((ScreenBuffer = (GifRowType *)
  129.     malloc(GifFile -> SHeight * sizeof(GifRowType *))) == NULL)
  130.         GIF_EXIT("Failed to allocate memory required, aborted.");
  131.  
  132.     Size = GifFile -> SWidth * sizeof(GifPixelType);/* Size in bytes one row.*/
  133.     if ((ScreenBuffer[0] = (GifRowType) malloc(Size)) == NULL) /* First row. */
  134.     GIF_EXIT("Failed to allocate memory required, aborted.");
  135.  
  136.     for (i = 0; i < GifFile -> SWidth; i++)  /* Set its color to BackGround. */
  137.     ScreenBuffer[0][i] = GifFile -> SBackGroundColor;
  138.     for (i = 1; i < GifFile -> SHeight; i++) {
  139.     /* Allocate the other rows, and set their color to background too: */
  140.     if ((ScreenBuffer[i] = (GifRowType) malloc(Size)) == NULL)
  141.         GIF_EXIT("Failed to allocate memory required, aborted.");
  142.  
  143.     memcpy(ScreenBuffer[i], ScreenBuffer[0], Size);
  144.     }
  145.  
  146.     /* Scan the content of the GIF file and load the image(s) in: */
  147.     do {
  148.     if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) {
  149.         PrintGifError();
  150.         exit(-1);
  151.     }
  152.     switch (RecordType) {
  153.         case IMAGE_DESC_RECORD_TYPE:
  154.         if (DGifGetImageDesc(GifFile) == GIF_ERROR) {
  155.             PrintGifError();
  156.             exit(-1);
  157.         }
  158.         Row = GifFile -> ITop; /* Image Position relative to Screen. */
  159.         Col = GifFile -> ILeft;
  160.         Width = GifFile -> IWidth;
  161.         Height = GifFile -> IHeight;
  162.         GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]:     ",
  163.             PROGRAM_NAME, ++ImageNum, Col, Row, Width, Height);
  164.         if (GifFile -> ILeft + GifFile -> IWidth > GifFile -> SWidth ||
  165.            GifFile -> ITop + GifFile -> IHeight > GifFile -> SHeight) {
  166.             fprintf(stderr, "Image %d is not confined to screen dimension, aborted.\n");
  167.             exit(-2);
  168.         }
  169.         if (GifFile -> IInterlace) {
  170.             /* Need to perform 4 passes on the images: */
  171.             for (Count = i = 0; i < 4; i++)
  172.             for (j = Row + InterlacedOffset[i]; j < Row + Height;
  173.                          j += InterlacedJumps[i]) {
  174.                 GifQprintf("\b\b\b\b%-4d", Count++);
  175.                 if (DGifGetLine(GifFile, &ScreenBuffer[j][Col],
  176.                 Width) == GIF_ERROR) {
  177.                 PrintGifError();
  178.                 exit(-1);
  179.                 }
  180.             }
  181.         }
  182.         else {
  183.             for (i = 0; i < Height; i++) {
  184.             GifQprintf("\b\b\b\b%-4d", i);
  185.             if (DGifGetLine(GifFile, &ScreenBuffer[Row++][Col],
  186.                 Width) == GIF_ERROR) {
  187.                 PrintGifError();
  188.                 exit(-1);
  189.             }
  190.             }
  191.         }
  192.         break;
  193.         case EXTENSION_RECORD_TYPE:
  194.         /* Skip any extension blocks in file: */
  195.         if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) {
  196.             PrintGifError();
  197.             exit(-1);
  198.         }
  199.         while (Extension != NULL) {
  200.             if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
  201.             PrintGifError();
  202.             exit(-1);
  203.             }
  204.         }
  205.         break;
  206.         case TERMINATE_RECORD_TYPE:
  207.         break;
  208.         default:            /* Should be traps by DGifGetRecordType. */
  209.         break;
  210.     }
  211.     }
  212.     while (RecordType != TERMINATE_RECORD_TYPE);
  213.  
  214.     /* Lets display it - set the global variables required and do it: */
  215.     BackGround = GifFile -> SBackGroundColor;
  216.     ColorMap = (GifFile -> IColorMap ? GifFile -> IColorMap :
  217.                        GifFile -> SColorMap);
  218.     ColorMapSize = 1 << (GifFile -> IColorMap ? GifFile -> IBitsPerPixel :
  219.                                 GifFile -> SBitsPerPixel);
  220.     DumpScreen2Rle(ScreenBuffer, GifFile -> SWidth, GifFile -> SHeight);
  221.  
  222.     if (DGifCloseFile(GifFile) == GIF_ERROR) {
  223.     PrintGifError();
  224.     exit(-1);
  225.     }
  226. }
  227.  
  228. /******************************************************************************
  229. * The real dumping routine.                              *
  230. ******************************************************************************/
  231. static void DumpScreen2Rle(GifRowType *ScreenBuffer,
  232.                     int ScreenWidth, int ScreenHeight)
  233. {
  234.     int i, j;
  235.     char Comment[80];
  236.     rle_pixel *rows[4];
  237.     GifRowType GifRow;
  238.     static GifColorType
  239.     *ColorMapEntry;
  240.  
  241.     if (AlphaFlag) RLE_SET_BIT(rle_dflt_hdr, RLE_ALPHA);
  242.     rle_dflt_hdr.alpha = AlphaFlag != 0;
  243.     rle_dflt_hdr.rle_file = stdout;
  244.     rle_dflt_hdr.xmin = 0;
  245.     rle_dflt_hdr.ymin = 0;
  246.     rle_dflt_hdr.xmax = ScreenWidth - 1;
  247.     rle_dflt_hdr.ymax = ScreenHeight - 1;
  248.     sprintf(Comment, "origin=GIF format, %d colors.", ColorMapSize);
  249.     rle_putcom(Comment, &rle_dflt_hdr);
  250.     rle_put_setup(&rle_dflt_hdr);
  251.  
  252.     for (i = 0; i < 4; i++)
  253.     if ((rows[i] = (rle_pixel *) malloc(sizeof(rle_pixel) * ScreenWidth))
  254.                                      == NULL)
  255.         GIF_EXIT("Failed to allocated memory required, aborted.");
  256.  
  257.     if (AlphaFlag) {
  258.     /* Initial the alpha channel to full coverage: */
  259.     for (i = 0; i < ScreenWidth; i++) rows[0][i] = 255;
  260.     }
  261.  
  262.     for (i = 0; i < ScreenHeight; i++) {
  263.     /* Flip the image vertically as rle files start at the bollom... */
  264.     GifRow = ScreenBuffer[ScreenHeight - i - 1];
  265.     GifQprintf("\b\b\b\b%-4d", ScreenHeight - i);
  266.     for (j = 0; j < ScreenWidth; j++) {
  267.         ColorMapEntry = &ColorMap[GifRow[j]];
  268.         rows[1][j] = ColorMapEntry -> Red;
  269.         rows[2][j] = ColorMapEntry -> Green;
  270.         rows[3][j] = ColorMapEntry -> Blue;
  271.     }
  272.     rle_putrow( &rows[1], ScreenWidth, &rle_dflt_hdr );
  273.     }
  274.  
  275.     rle_puteof( &rle_dflt_hdr );
  276. }
  277.